﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DTO;

namespace DAO
{
    public class TopicDAO
    {
        public static List<TOPIC> getAllTopics()
        {
            List<TOPIC> res = null;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(GlobalShare.pathDatabase);

                XmlNodeList nodeTopics = xmlDoc.GetElementsByTagName("Topic");
                if (nodeTopics.Count <= 0)
                {
                    return res;
                }

                res = new List<TOPIC>();
                for (int i = 0; i < nodeTopics.Count; i++)
                {
                    TOPIC tempTopic = new TOPIC();
                    tempTopic.LoadTopic(nodeTopics[i]);
                    res.Add(tempTopic);
                }     
            }
            catch (Exception e)
            {
                throw e;
            }
            return res;
        }

        public static TOPIC getTopic(int idtopic)
        {            
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(GlobalShare.pathDatabase);

                XmlNodeList nodeTopics = xmlDoc.GetElementsByTagName("Topic");
                if (nodeTopics.Count <= 0)
                {
                    return null;
                }
                
                for (int i = 0; i < nodeTopics.Count; i++)
                {                    
                    TOPIC tempTopic = new TOPIC();
                    tempTopic.LoadTopic(nodeTopics[i]);
                    if (tempTopic.idTopic == idtopic)
                    {
                        return tempTopic;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return null;
        }

        public static TOPIC getTopic(String name)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(GlobalShare.pathDatabase);

                XmlNodeList nodeTopics = xmlDoc.GetElementsByTagName("Topic");
                if (nodeTopics.Count <= 0)
                {
                    return null;
                }

                for (int i = 0; i < nodeTopics.Count; i++)
                {
                    TOPIC tempTopic = new TOPIC();
                    tempTopic.LoadTopic(nodeTopics[i]);
                    if (tempTopic.Name.Equals(name))
                    {
                        return tempTopic;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return null;
        }

        public static bool Add(TOPIC tp)
        {
            bool res = false;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(GlobalShare.pathDatabase);

                XmlNode nodeTopics = xmlDoc.SelectSingleNode("Topics");

                if (nodeTopics == null)
                {
                    return false;
                }

                XmlNode nodeTpNew = xmlDoc.CreateElement("Topic");
                XmlNode nodeTemp;
                
                nodeTemp = xmlDoc.CreateElement("idTopic");
                nodeTemp.Value = tp.idTopic.ToString();
                nodeTpNew.AppendChild(nodeTemp);

                nodeTemp = xmlDoc.CreateElement("Name");
                nodeTemp.Value = tp.Name;
                nodeTpNew.AppendChild(nodeTemp);                

                nodeTopics.AppendChild(nodeTpNew);
            }
            catch (Exception e)
            {
                throw e;
            }
            return res;
        }

        public static bool Edit(int idtopic, TOPIC tpUpdate)
        {
            bool res = false;
            try
            {
                
            }
            catch (Exception e)
            {
                throw e;
            }
            return res;
        }

        public static bool Remove(TOPIC tp)
        {
            bool res = false;
            try
            {
                
            }
            catch (Exception e)
            {
                throw e;
            }
            return res;
        }
    }
}
